package model

import "git.inspur.com/x/models"

type Order struct {
    Id         string      `xorm:"pk" json:"id" valid:"-" `
    AccountId  string      `json:"accountId" valid:"-"`
    OrderId    string      `json:"orderId" valid:"-"`
    ConsoleId  string      `json:"consoleId" valid:"-"`
    InstanceId string      `json:"instanceId" valid:"-"`
    Status     string      `json:"status" valid:"-"` // 订单状态 normal, stop
    CreateTime models.Time `xorm:"created" json:"createTime" valid:"-" `
    UpdateTime models.Time `xorm:"updated" json:"updateTime" valid:"-" `
    DeleteTime models.Time `xorm:"deleted" json:"deleteTime" valid:"-" `
}

func CreateOrder(order *Order) (*Order, error) {
    if _, err := models.Engine.Insert(order); err != nil {
        _ = logger.Error("CreateOrder: insert error: %v", err)
        return nil, err
    }
    return order, nil
}

func UpdateOrderStatus(instanceId string, status string) error {
    _, err := models.Engine.Table(new(Order)).Where("instance_id=?", instanceId).Update(map[string]interface{}{
        "status": status,
    })
    return err
}

func GetAllOrder() ([]*Order, error) {
    orderList := make([]*Order, 0)
    err := models.Engine.Find(&orderList)
    if err != nil {
        _ = logger.Error("GetAllOrder: get order list error %v", err)
        return nil, err
    }
    return orderList, nil
}

func GetOrderByAccountId(id string) (*Order, error) {
    o := &Order{}
    if _, err := models.Engine.Where("account_id=?", id).Get(o); err != nil {
        _ = logger.Error("GetOrderByAccountId: get error, %s", err.Error())
        return nil, err
    }
    return o, nil
}

func (o Order) TableName() string {
    return "fcs_order"
}
